package com.yc.javaee.tomcat.impl;

import com.yc.javaee.tomcat.HttpServletRequest;
import com.yc.javaee.tomcat.HttpServletResponse;
import com.yc.javaee.tomcat.HttpSession;
import com.yc.javaee.tomcat.ServletException;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/session.s")
public class SessionServlet extends HttpServletImpl {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession();
        Object mydata = session.getAttribute("mydata");
        response.setStatus(200, "OK");
        response.setContentType("text/html;charset=utf-8");
        if (mydata == null) {
            session.setAttribute("mydata", "今天天气很好");
            response.getWriter().append("没有会话数据!");
        } else {
            response.getWriter().append("mydata: " + mydata);
        }
    }
}
